home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / dist.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  38KB  |  1,066 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.dist
  5.  
  6. Provides the Distribution class, which represents the module distribution
  7. being built/installed/distributed.
  8. '''
  9. __revision__ = '$Id: dist.py 68035 2008-12-29 22:36:22Z tarek.ziade $'
  10. import sys
  11. import os
  12. import string
  13. import re
  14. from types import *
  15. from copy import copy
  16.  
  17. try:
  18.     import warnings
  19. except ImportError:
  20.     warnings = None
  21.  
  22. from distutils.errors import *
  23. from distutils.fancy_getopt import FancyGetopt, translate_longopt
  24. from distutils.util import check_environ, strtobool, rfc822_escape
  25. from distutils import log
  26. from distutils.debug import DEBUG
  27. PKG_INFO_ENCODING = 'utf-8'
  28. command_re = re.compile('^[a-zA-Z]([a-zA-Z0-9_]*)$')
  29.  
  30. class Distribution:
  31.     """The core of the Distutils.  Most of the work hiding behind 'setup'
  32.     is really done within a Distribution instance, which farms the work out
  33.     to the Distutils commands specified on the command line.
  34.  
  35.     Setup scripts will almost never instantiate Distribution directly,
  36.     unless the 'setup()' function is totally inadequate to their needs.
  37.     However, it is conceivable that a setup script might wish to subclass
  38.     Distribution for some specialized purpose, and then pass the subclass
  39.     to 'setup()' as the 'distclass' keyword argument.  If so, it is
  40.     necessary to respect the expectations that 'setup' has of Distribution.
  41.     See the code for 'setup()', in core.py, for details.
  42.     """
  43.     global_options = [
  44.         ('verbose', 'v', 'run verbosely (default)', 1),
  45.         ('quiet', 'q', 'run quietly (turns verbosity off)'),
  46.         ('dry-run', 'n', "don't actually do anything"),
  47.         ('help', 'h', 'show detailed help message')]
  48.     common_usage = "Common commands: (see '--help-commands' for more)\n\n  setup.py build      will build the package underneath 'build/'\n  setup.py install    will install the package\n"
  49.     display_options = [
  50.         ('help-commands', None, 'list all available commands'),
  51.         ('name', None, 'print package name'),
  52.         ('version', 'V', 'print package version'),
  53.         ('fullname', None, 'print <package name>-<version>'),
  54.         ('author', None, "print the author's name"),
  55.         ('author-email', None, "print the author's email address"),
  56.         ('maintainer', None, "print the maintainer's name"),
  57.         ('maintainer-email', None, "print the maintainer's email address"),
  58.         ('contact', None, "print the maintainer's name if known, else the author's"),
  59.         ('contact-email', None, "print the maintainer's email address if known, else the author's"),
  60.         ('url', None, 'print the URL for this package'),
  61.         ('license', None, 'print the license of the package'),
  62.         ('licence', None, 'alias for --license'),
  63.         ('description', None, 'print the package description'),
  64.         ('long-description', None, 'print the long package description'),
  65.         ('platforms', None, 'print the list of platforms'),
  66.         ('classifiers', None, 'print the list of classifiers'),
  67.         ('keywords', None, 'print the list of keywords'),
  68.         ('provides', None, 'print the list of packages/modules provided'),
  69.         ('requires', None, 'print the list of packages/modules required'),
  70.         ('obsoletes', None, 'print the list of packages/modules made obsolete')]
  71.     display_option_names = map((lambda x: translate_longopt(x[0])), display_options)
  72.     negative_opt = {
  73.         'quiet': 'verbose' }
  74.     
  75.     def __init__(self, attrs = None):
  76.         '''Construct a new Distribution instance: initialize all the
  77.         attributes of a Distribution, and then use \'attrs\' (a dictionary
  78.         mapping attribute names to values) to assign some of those
  79.         attributes their "real" values.  (Any attributes not mentioned in
  80.         \'attrs\' will be assigned to some null value: 0, None, an empty list
  81.         or dictionary, etc.)  Most importantly, initialize the
  82.         \'command_obj\' attribute to the empty dictionary; this will be
  83.         filled in with real command objects by \'parse_command_line()\'.
  84.         '''
  85.         self.verbose = 1
  86.         self.dry_run = 0
  87.         self.help = 0
  88.         for attr in self.display_option_names:
  89.             setattr(self, attr, 0)
  90.         
  91.         self.metadata = DistributionMetadata()
  92.         for basename in self.metadata._METHOD_BASENAMES:
  93.             method_name = 'get_' + basename
  94.             setattr(self, method_name, getattr(self.metadata, method_name))
  95.         
  96.         self.cmdclass = { }
  97.         self.command_packages = None
  98.         self.script_name = None
  99.         self.script_args = None
  100.         self.command_options = { }
  101.         self.dist_files = []
  102.         self.packages = None
  103.         self.package_data = { }
  104.         self.package_dir = None
  105.         self.py_modules = None
  106.         self.libraries = None
  107.         self.headers = None
  108.         self.ext_modules = None
  109.         self.ext_package = None
  110.         self.include_dirs = None
  111.         self.extra_path = None
  112.         self.scripts = None
  113.         self.data_files = None
  114.         self.command_obj = { }
  115.         self.have_run = { }
  116.         if attrs:
  117.             options = attrs.get('options')
  118.             if options is not None:
  119.                 del attrs['options']
  120.                 for command, cmd_options in options.items():
  121.                     opt_dict = self.get_option_dict(command)
  122.                     for opt, val in cmd_options.items():
  123.                         opt_dict[opt] = ('setup script', val)
  124.                     
  125.                 
  126.             
  127.             if 'licence' in attrs:
  128.                 attrs['license'] = attrs['licence']
  129.                 del attrs['licence']
  130.                 msg = "'licence' distribution option is deprecated; use 'license'"
  131.                 if warnings is not None:
  132.                     warnings.warn(msg)
  133.                 else:
  134.                     sys.stderr.write(msg + '\n')
  135.             
  136.             for key, val in attrs.items():
  137.                 if hasattr(self.metadata, 'set_' + key):
  138.                     getattr(self.metadata, 'set_' + key)(val)
  139.                     continue
  140.                 if hasattr(self.metadata, key):
  141.                     setattr(self.metadata, key, val)
  142.                     continue
  143.                 if hasattr(self, key):
  144.                     setattr(self, key, val)
  145.                     continue
  146.                 msg = 'Unknown distribution option: %s' % repr(key)
  147.                 if warnings is not None:
  148.                     warnings.warn(msg)
  149.                     continue
  150.                 sys.stderr.write(msg + '\n')
  151.             
  152.         
  153.         self.finalize_options()
  154.  
  155.     
  156.     def get_option_dict(self, command):
  157.         """Get the option dictionary for a given command.  If that
  158.         command's option dictionary hasn't been created yet, then create it
  159.         and return the new dictionary; otherwise, return the existing
  160.         option dictionary.
  161.         """
  162.         dict = self.command_options.get(command)
  163.         if dict is None:
  164.             dict = self.command_options[command] = { }
  165.         
  166.         return dict
  167.  
  168.     
  169.     def dump_option_dicts(self, header = None, commands = None, indent = ''):
  170.         pformat = pformat
  171.         import pprint
  172.         if commands is None:
  173.             commands = self.command_options.keys()
  174.             commands.sort()
  175.         
  176.         if header is not None:
  177.             print indent + header
  178.             indent = indent + '  '
  179.         
  180.         if not commands:
  181.             print indent + 'no commands known yet'
  182.             return None
  183.         for cmd_name in commands:
  184.             opt_dict = self.command_options.get(cmd_name)
  185.             if opt_dict is None:
  186.                 print indent + "no option dict for '%s' command" % cmd_name
  187.                 continue
  188.             print indent + "option dict for '%s' command:" % cmd_name
  189.             out = pformat(opt_dict)
  190.             for line in string.split(out, '\n'):
  191.                 print indent + '  ' + line
  192.             
  193.         
  194.  
  195.     
  196.     def find_config_files(self):
  197.         """Find as many configuration files as should be processed for this
  198.         platform, and return a list of filenames in the order in which they
  199.         should be parsed.  The filenames returned are guaranteed to exist
  200.         (modulo nasty race conditions).
  201.  
  202.         There are three possible config files: distutils.cfg in the
  203.         Distutils installation directory (ie. where the top-level
  204.         Distutils __inst__.py file lives), a file in the user's home
  205.         directory named .pydistutils.cfg on Unix and pydistutils.cfg
  206.         on Windows/Mac, and setup.cfg in the current directory.
  207.         """
  208.         files = []
  209.         check_environ()
  210.         sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
  211.         sys_file = os.path.join(sys_dir, 'distutils.cfg')
  212.         if os.path.isfile(sys_file):
  213.             files.append(sys_file)
  214.         
  215.         if os.name == 'posix':
  216.             user_filename = '.pydistutils.cfg'
  217.         else:
  218.             user_filename = 'pydistutils.cfg'
  219.         user_file = os.path.join(os.path.expanduser('~'), user_filename)
  220.         if os.path.isfile(user_file):
  221.             files.append(user_file)
  222.         
  223.         local_file = 'setup.cfg'
  224.         if os.path.isfile(local_file):
  225.             files.append(local_file)
  226.         
  227.         return files
  228.  
  229.     
  230.     def parse_config_files(self, filenames = None):
  231.         ConfigParser = ConfigParser
  232.         import ConfigParser
  233.         if filenames is None:
  234.             filenames = self.find_config_files()
  235.         
  236.         if DEBUG:
  237.             print 'Distribution.parse_config_files():'
  238.         
  239.         parser = ConfigParser()
  240.         for filename in filenames:
  241.             if DEBUG:
  242.                 print '  reading', filename
  243.             
  244.             parser.read(filename)
  245.             for section in parser.sections():
  246.                 options = parser.options(section)
  247.                 opt_dict = self.get_option_dict(section)
  248.                 for opt in options:
  249.                     if opt != '__name__':
  250.                         val = parser.get(section, opt)
  251.                         opt = string.replace(opt, '-', '_')
  252.                         opt_dict[opt] = (filename, val)
  253.                         continue
  254.                 
  255.             
  256.             parser.__init__()
  257.         
  258.  
  259.     
  260.     def parse_command_line(self):
  261.         '''Parse the setup script\'s command line, taken from the
  262.         \'script_args\' instance attribute (which defaults to \'sys.argv[1:]\'
  263.         -- see \'setup()\' in core.py).  This list is first processed for
  264.         "global options" -- options that set attributes of the Distribution
  265.         instance.  Then, it is alternately scanned for Distutils commands
  266.         and options for that command.  Each new command terminates the
  267.         options for the previous command.  The allowed options for a
  268.         command are determined by the \'user_options\' attribute of the
  269.         command class -- thus, we have to be able to load command classes
  270.         in order to parse the command line.  Any error in that \'options\'
  271.         attribute raises DistutilsGetoptError; any error on the
  272.         command-line raises DistutilsArgError.  If no Distutils commands
  273.         were found on the command line, raises DistutilsArgError.  Return
  274.         true if command-line was successfully parsed and we should carry
  275.         on with executing commands; false if no errors but we shouldn\'t
  276.         execute commands (currently, this only happens if user asks for
  277.         help).
  278.         '''
  279.         toplevel_options = self._get_toplevel_options()
  280.         if sys.platform == 'mac':
  281.             import EasyDialogs as EasyDialogs
  282.             cmdlist = self.get_command_list()
  283.             self.script_args = EasyDialogs.GetArgv(toplevel_options + self.display_options, cmdlist)
  284.         
  285.         self.commands = []
  286.         parser = FancyGetopt(toplevel_options + self.display_options)
  287.         parser.set_negative_aliases(self.negative_opt)
  288.         parser.set_aliases({
  289.             'licence': 'license' })
  290.         args = parser.getopt(args = self.script_args, object = self)
  291.         option_order = parser.get_option_order()
  292.         log.set_verbosity(self.verbose)
  293.         if self.handle_display_options(option_order):
  294.             return None
  295.         while args:
  296.             args = self._parse_command_opts(parser, args)
  297.             if args is None:
  298.                 return None
  299.             continue
  300.             args is None
  301.         if self.help:
  302.             self._show_help(parser, display_options = len(self.commands) == 0, commands = self.commands)
  303.             return None
  304.         if not self.commands:
  305.             raise DistutilsArgError, 'no commands supplied'
  306.         self.commands
  307.         return 1
  308.  
  309.     
  310.     def _get_toplevel_options(self):
  311.         '''Return the non-display options recognized at the top level.
  312.  
  313.         This includes options that are recognized *only* at the top
  314.         level as well as options recognized for commands.
  315.         '''
  316.         return self.global_options + [
  317.             ('command-packages=', None, 'list of packages that provide distutils commands')]
  318.  
  319.     
  320.     def _parse_command_opts(self, parser, args):
  321.         """Parse the command-line options for a single command.
  322.         'parser' must be a FancyGetopt instance; 'args' must be the list
  323.         of arguments, starting with the current command (whose options
  324.         we are about to parse).  Returns a new version of 'args' with
  325.         the next command at the front of the list; will be the empty
  326.         list if there are no more commands on the command line.  Returns
  327.         None if the user asked for help on this command.
  328.         """
  329.         Command = Command
  330.         import distutils.cmd
  331.         command = args[0]
  332.         if not command_re.match(command):
  333.             raise SystemExit, "invalid command name '%s'" % command
  334.         command_re.match(command)
  335.         self.commands.append(command)
  336.         
  337.         try:
  338.             cmd_class = self.get_command_class(command)
  339.         except DistutilsModuleError:
  340.             msg = None
  341.             raise DistutilsArgError, msg
  342.  
  343.         if not issubclass(cmd_class, Command):
  344.             raise DistutilsClassError, 'command class %s must subclass Command' % cmd_class
  345.         issubclass(cmd_class, Command)
  346.         if not hasattr(cmd_class, 'user_options') and type(cmd_class.user_options) is ListType:
  347.             raise DistutilsClassError, ('command class %s must provide ' + "'user_options' attribute (a list of tuples)") % cmd_class
  348.         type(cmd_class.user_options) is ListType
  349.         negative_opt = self.negative_opt
  350.         if hasattr(cmd_class, 'negative_opt'):
  351.             negative_opt = copy(negative_opt)
  352.             negative_opt.update(cmd_class.negative_opt)
  353.         
  354.         if hasattr(cmd_class, 'help_options') and type(cmd_class.help_options) is ListType:
  355.             help_options = fix_help_options(cmd_class.help_options)
  356.         else:
  357.             help_options = []
  358.         parser.set_option_table(self.global_options + cmd_class.user_options + help_options)
  359.         parser.set_negative_aliases(negative_opt)
  360.         (args, opts) = parser.getopt(args[1:])
  361.         if hasattr(opts, 'help') and opts.help:
  362.             self._show_help(parser, display_options = 0, commands = [
  363.                 cmd_class])
  364.             return None
  365.         opt_dict = self.get_option_dict(command)
  366.         for name, value in vars(opts).items():
  367.             opt_dict[name] = ('command line', value)
  368.         
  369.         return args
  370.  
  371.     
  372.     def finalize_options(self):
  373.         '''Set final values for all the options on the Distribution
  374.         instance, analogous to the .finalize_options() method of Command
  375.         objects.
  376.         '''
  377.         keywords = self.metadata.keywords
  378.         if keywords is not None:
  379.             if type(keywords) is StringType:
  380.                 keywordlist = string.split(keywords, ',')
  381.                 self.metadata.keywords = map(string.strip, keywordlist)
  382.             
  383.         
  384.         platforms = self.metadata.platforms
  385.         if platforms is not None:
  386.             if type(platforms) is StringType:
  387.                 platformlist = string.split(platforms, ',')
  388.                 self.metadata.platforms = map(string.strip, platformlist)
  389.             
  390.         
  391.  
  392.     
  393.     def _show_help(self, parser, global_options = 1, display_options = 1, commands = []):
  394.         '''Show help for the setup script command-line in the form of
  395.         several lists of command-line options.  \'parser\' should be a
  396.         FancyGetopt instance; do not expect it to be returned in the
  397.         same state, as its option table will be reset to make it
  398.         generate the correct help text.
  399.  
  400.         If \'global_options\' is true, lists the global options:
  401.         --verbose, --dry-run, etc.  If \'display_options\' is true, lists
  402.         the "display-only" options: --name, --version, etc.  Finally,
  403.         lists per-command help for every command name or command class
  404.         in \'commands\'.
  405.         '''
  406.         gen_usage = gen_usage
  407.         import distutils.core
  408.         Command = Command
  409.         import distutils.cmd
  410.         if global_options:
  411.             if display_options:
  412.                 options = self._get_toplevel_options()
  413.             else:
  414.                 options = self.global_options
  415.             parser.set_option_table(options)
  416.             parser.print_help(self.common_usage + '\nGlobal options:')
  417.             print 
  418.         
  419.         if display_options:
  420.             parser.set_option_table(self.display_options)
  421.             parser.print_help('Information display options (just display ' + 'information, ignore any commands)')
  422.             print 
  423.         
  424.         for command in self.commands:
  425.             if type(command) is ClassType and issubclass(command, Command):
  426.                 klass = command
  427.             else:
  428.                 klass = self.get_command_class(command)
  429.             if hasattr(klass, 'help_options') and type(klass.help_options) is ListType:
  430.                 parser.set_option_table(klass.user_options + fix_help_options(klass.help_options))
  431.             else:
  432.                 parser.set_option_table(klass.user_options)
  433.             parser.print_help("Options for '%s' command:" % klass.__name__)
  434.             print 
  435.         
  436.         print gen_usage(self.script_name)
  437.  
  438.     
  439.     def handle_display_options(self, option_order):
  440.         '''If there were any non-global "display-only" options
  441.         (--help-commands or the metadata display options) on the command
  442.         line, display the requested info and return true; else return
  443.         false.
  444.         '''
  445.         gen_usage = gen_usage
  446.         import distutils.core
  447.         if self.help_commands:
  448.             self.print_commands()
  449.             print 
  450.             print gen_usage(self.script_name)
  451.             return 1
  452.         any_display_options = 0
  453.         is_display_option = { }
  454.         for option in self.display_options:
  455.             is_display_option[option[0]] = 1
  456.         
  457.         for opt, val in option_order:
  458.             if val and is_display_option.get(opt):
  459.                 opt = translate_longopt(opt)
  460.                 value = getattr(self.metadata, 'get_' + opt)()
  461.                 if opt in ('keywords', 'platforms'):
  462.                     print string.join(value, ',')
  463.                 elif opt in ('classifiers', 'provides', 'requires', 'obsoletes'):
  464.                     print string.join(value, '\n')
  465.                 else:
  466.                     print value
  467.                 any_display_options = 1
  468.                 continue
  469.         
  470.         return any_display_options
  471.  
  472.     
  473.     def print_command_list(self, commands, header, max_length):
  474.         """Print a subset of the list of all commands -- used by
  475.         'print_commands()'.
  476.         """
  477.         print header + ':'
  478.         for cmd in commands:
  479.             klass = self.cmdclass.get(cmd)
  480.             if not klass:
  481.                 klass = self.get_command_class(cmd)
  482.             
  483.             
  484.             try:
  485.                 description = klass.description
  486.             except AttributeError:
  487.                 description = '(no description available)'
  488.  
  489.             print '  %-*s  %s' % (max_length, cmd, description)
  490.         
  491.  
  492.     
  493.     def print_commands(self):
  494.         '''Print out a help message listing all available commands with a
  495.         description of each.  The list is divided into "standard commands"
  496.         (listed in distutils.command.__all__) and "extra commands"
  497.         (mentioned in self.cmdclass, but not a standard command).  The
  498.         descriptions come from the command class attribute
  499.         \'description\'.
  500.         '''
  501.         import distutils.command as distutils
  502.         std_commands = distutils.command.__all__
  503.         is_std = { }
  504.         for cmd in std_commands:
  505.             is_std[cmd] = 1
  506.         
  507.         extra_commands = []
  508.         for cmd in self.cmdclass.keys():
  509.             if not is_std.get(cmd):
  510.                 extra_commands.append(cmd)
  511.                 continue
  512.         
  513.         max_length = 0
  514.         for cmd in std_commands + extra_commands:
  515.             if len(cmd) > max_length:
  516.                 max_length = len(cmd)
  517.                 continue
  518.         
  519.         self.print_command_list(std_commands, 'Standard commands', max_length)
  520.         if extra_commands:
  521.             print 
  522.             self.print_command_list(extra_commands, 'Extra commands', max_length)
  523.         
  524.  
  525.     
  526.     def get_command_list(self):
  527.         '''Get a list of (command, description) tuples.
  528.         The list is divided into "standard commands" (listed in
  529.         distutils.command.__all__) and "extra commands" (mentioned in
  530.         self.cmdclass, but not a standard command).  The descriptions come
  531.         from the command class attribute \'description\'.
  532.         '''
  533.         import distutils.command as distutils
  534.         std_commands = distutils.command.__all__
  535.         is_std = { }
  536.         for cmd in std_commands:
  537.             is_std[cmd] = 1
  538.         
  539.         extra_commands = []
  540.         for cmd in self.cmdclass.keys():
  541.             if not is_std.get(cmd):
  542.                 extra_commands.append(cmd)
  543.                 continue
  544.         
  545.         rv = []
  546.         for cmd in std_commands + extra_commands:
  547.             klass = self.cmdclass.get(cmd)
  548.             if not klass:
  549.                 klass = self.get_command_class(cmd)
  550.             
  551.             
  552.             try:
  553.                 description = klass.description
  554.             except AttributeError:
  555.                 description = '(no description available)'
  556.  
  557.             rv.append((cmd, description))
  558.         
  559.         return rv
  560.  
  561.     
  562.     def get_command_packages(self):
  563.         '''Return a list of packages from which commands are loaded.'''
  564.         pkgs = self.command_packages
  565.         if not isinstance(pkgs, type([])):
  566.             if not pkgs:
  567.                 pass
  568.             pkgs = string.split('', ',')
  569.             for i in range(len(pkgs)):
  570.                 pkgs[i] = string.strip(pkgs[i])
  571.             
  572.             pkgs = filter(None, pkgs)
  573.             if 'distutils.command' not in pkgs:
  574.                 pkgs.insert(0, 'distutils.command')
  575.             
  576.             self.command_packages = pkgs
  577.         
  578.         return pkgs
  579.  
  580.     
  581.     def get_command_class(self, command):
  582.         '''Return the class that implements the Distutils command named by
  583.         \'command\'.  First we check the \'cmdclass\' dictionary; if the
  584.         command is mentioned there, we fetch the class object from the
  585.         dictionary and return it.  Otherwise we load the command module
  586.         ("distutils.command." + command) and fetch the command class from
  587.         the module.  The loaded class is also stored in \'cmdclass\'
  588.         to speed future calls to \'get_command_class()\'.
  589.  
  590.         Raises DistutilsModuleError if the expected module could not be
  591.         found, or if that module does not define the expected class.
  592.         '''
  593.         klass = self.cmdclass.get(command)
  594.         if klass:
  595.             return klass
  596.         for pkgname in self.get_command_packages():
  597.             module_name = '%s.%s' % (pkgname, command)
  598.             klass_name = command
  599.             
  600.             try:
  601.                 __import__(module_name)
  602.                 module = sys.modules[module_name]
  603.             except ImportError:
  604.                 klass
  605.                 klass
  606.                 continue
  607.             except:
  608.                 klass
  609.  
  610.             
  611.             try:
  612.                 klass = getattr(module, klass_name)
  613.             except AttributeError:
  614.                 klass
  615.                 klass
  616.                 raise DistutilsModuleError, "invalid command '%s' (no class '%s' in module '%s')" % (command, klass_name, module_name)
  617.             except:
  618.                 klass
  619.  
  620.             self.cmdclass[command] = klass
  621.             return klass
  622.         
  623.         raise DistutilsModuleError("invalid command '%s'" % command)
  624.  
  625.     
  626.     def get_command_obj(self, command, create = 1):
  627.         """Return the command object for 'command'.  Normally this object
  628.         is cached on a previous call to 'get_command_obj()'; if no command
  629.         object for 'command' is in the cache, then we either create and
  630.         return it (if 'create' is true) or return None.
  631.         """
  632.         cmd_obj = self.command_obj.get(command)
  633.         if not cmd_obj and create:
  634.             if DEBUG:
  635.                 print "Distribution.get_command_obj(): creating '%s' command object" % command
  636.             
  637.             klass = self.get_command_class(command)
  638.             cmd_obj = self.command_obj[command] = klass(self)
  639.             self.have_run[command] = 0
  640.             options = self.command_options.get(command)
  641.             if options:
  642.                 self._set_command_options(cmd_obj, options)
  643.             
  644.         
  645.         return cmd_obj
  646.  
  647.     
  648.     def _set_command_options(self, command_obj, option_dict = None):
  649.         """Set the options for 'command_obj' from 'option_dict'.  Basically
  650.         this means copying elements of a dictionary ('option_dict') to
  651.         attributes of an instance ('command').
  652.  
  653.         'command_obj' must be a Command instance.  If 'option_dict' is not
  654.         supplied, uses the standard option dictionary for this command
  655.         (from 'self.command_options').
  656.         """
  657.         command_name = command_obj.get_command_name()
  658.         if option_dict is None:
  659.             option_dict = self.get_option_dict(command_name)
  660.         
  661.         if DEBUG:
  662.             print "  setting options for '%s' command:" % command_name
  663.         
  664.         for source, value in option_dict.items():
  665.             
  666.             try:
  667.                 bool_opts = map(translate_longopt, command_obj.boolean_options)
  668.             except AttributeError:
  669.                 None if DEBUG else None
  670.                 None if DEBUG else None
  671.                 bool_opts = []
  672.             except:
  673.                 None if DEBUG else None
  674.  
  675.             
  676.             try:
  677.                 neg_opt = command_obj.negative_opt
  678.             except AttributeError:
  679.                 None if DEBUG else None
  680.                 None if DEBUG else None
  681.                 neg_opt = { }
  682.             except:
  683.                 None if DEBUG else None
  684.  
  685.             
  686.             try:
  687.                 is_string = type(value) is StringType
  688.                 if option in neg_opt and is_string:
  689.                     setattr(command_obj, neg_opt[option], not strtobool(value))
  690.                 elif option in bool_opts and is_string:
  691.                     setattr(command_obj, option, strtobool(value))
  692.                 elif hasattr(command_obj, option):
  693.                     setattr(command_obj, option, value)
  694.                 else:
  695.                     raise DistutilsOptionError, "error in %s: command '%s' has no such option '%s'" % (source, command_name, option)
  696.                 continue
  697.                 except ValueError:
  698.                     is_string
  699.                     msg = is_string
  700.                     raise DistutilsOptionError, msg
  701.                     continue
  702.                 
  703.             return None
  704.  
  705.  
  706.     
  707.     def reinitialize_command(self, command, reinit_subcommands = 0):
  708.         '''Reinitializes a command to the state it was in when first
  709.         returned by \'get_command_obj()\': ie., initialized but not yet
  710.         finalized.  This provides the opportunity to sneak option
  711.         values in programmatically, overriding or supplementing
  712.         user-supplied values from the config files and command line.
  713.         You\'ll have to re-finalize the command object (by calling
  714.         \'finalize_options()\' or \'ensure_finalized()\') before using it for
  715.         real.
  716.  
  717.         \'command\' should be a command name (string) or command object.  If
  718.         \'reinit_subcommands\' is true, also reinitializes the command\'s
  719.         sub-commands, as declared by the \'sub_commands\' class attribute (if
  720.         it has one).  See the "install" command for an example.  Only
  721.         reinitializes the sub-commands that actually matter, ie. those
  722.         whose test predicates return true.
  723.  
  724.         Returns the reinitialized command object.
  725.         '''
  726.         Command = Command
  727.         import distutils.cmd
  728.         if not isinstance(command, Command):
  729.             command_name = command
  730.             command = self.get_command_obj(command_name)
  731.         else:
  732.             command_name = command.get_command_name()
  733.         if not command.finalized:
  734.             return command
  735.         command.initialize_options()
  736.         command.finalized = 0
  737.         self.have_run[command_name] = 0
  738.         self._set_command_options(command)
  739.         if reinit_subcommands:
  740.             for sub in command.get_sub_commands():
  741.                 self.reinitialize_command(sub, reinit_subcommands)
  742.             
  743.         
  744.         return command
  745.  
  746.     
  747.     def announce(self, msg, level = 1):
  748.         log.debug(msg)
  749.  
  750.     
  751.     def run_commands(self):
  752.         """Run each command that was seen on the setup script command line.
  753.         Uses the list of commands found and cache of command objects
  754.         created by 'get_command_obj()'.
  755.         """
  756.         for cmd in self.commands:
  757.             self.run_command(cmd)
  758.         
  759.  
  760.     
  761.     def run_command(self, command):
  762.         """Do whatever it takes to run a command (including nothing at all,
  763.         if the command has already been run).  Specifically: if we have
  764.         already created and run the command named by 'command', return
  765.         silently without doing anything.  If the command named by 'command'
  766.         doesn't even have a command object yet, create one.  Then invoke
  767.         'run()' on that command object (or an existing one).
  768.         """
  769.         if self.have_run.get(command):
  770.             return None
  771.         log.info('running %s', command)
  772.         cmd_obj = self.get_command_obj(command)
  773.         cmd_obj.ensure_finalized()
  774.         cmd_obj.run()
  775.         self.have_run[command] = 1
  776.  
  777.     
  778.     def has_pure_modules(self):
  779.         if not self.packages and self.py_modules:
  780.             pass
  781.         return len([]) > 0
  782.  
  783.     
  784.     def has_ext_modules(self):
  785.         if self.ext_modules:
  786.             pass
  787.         return len(self.ext_modules) > 0
  788.  
  789.     
  790.     def has_c_libraries(self):
  791.         if self.libraries:
  792.             pass
  793.         return len(self.libraries) > 0
  794.  
  795.     
  796.     def has_modules(self):
  797.         if not self.has_pure_modules():
  798.             pass
  799.         return self.has_ext_modules()
  800.  
  801.     
  802.     def has_headers(self):
  803.         if self.headers:
  804.             pass
  805.         return len(self.headers) > 0
  806.  
  807.     
  808.     def has_scripts(self):
  809.         if self.scripts:
  810.             pass
  811.         return len(self.scripts) > 0
  812.  
  813.     
  814.     def has_data_files(self):
  815.         if self.data_files:
  816.             pass
  817.         return len(self.data_files) > 0
  818.  
  819.     
  820.     def is_pure(self):
  821.         if self.has_pure_modules() and not self.has_ext_modules():
  822.             pass
  823.         return not self.has_c_libraries()
  824.  
  825.  
  826.  
  827. class DistributionMetadata:
  828.     '''Dummy class to hold the distribution meta-data: name, version,
  829.     author, and so forth.
  830.     '''
  831.     _METHOD_BASENAMES = ('name', 'version', 'author', 'author_email', 'maintainer', 'maintainer_email', 'url', 'license', 'description', 'long_description', 'keywords', 'platforms', 'fullname', 'contact', 'contact_email', 'license', 'classifiers', 'download_url', 'provides', 'requires', 'obsoletes')
  832.     
  833.     def __init__(self):
  834.         self.name = None
  835.         self.version = None
  836.         self.author = None
  837.         self.author_email = None
  838.         self.maintainer = None
  839.         self.maintainer_email = None
  840.         self.url = None
  841.         self.license = None
  842.         self.description = None
  843.         self.long_description = None
  844.         self.keywords = None
  845.         self.platforms = None
  846.         self.classifiers = None
  847.         self.download_url = None
  848.         self.provides = None
  849.         self.requires = None
  850.         self.obsoletes = None
  851.  
  852.     
  853.     def write_pkg_info(self, base_dir):
  854.         '''Write the PKG-INFO file into the release tree.
  855.         '''
  856.         pkg_info = open(os.path.join(base_dir, 'PKG-INFO'), 'w')
  857.         self.write_pkg_file(pkg_info)
  858.         pkg_info.close()
  859.  
  860.     
  861.     def write_pkg_file(self, file):
  862.         '''Write the PKG-INFO format data to a file object.
  863.         '''
  864.         version = '1.0'
  865.         if self.provides and self.requires or self.obsoletes:
  866.             version = '1.1'
  867.         
  868.         self._write_field(file, 'Metadata-Version', version)
  869.         self._write_field(file, 'Name', self.get_name())
  870.         self._write_field(file, 'Version', self.get_version())
  871.         self._write_field(file, 'Summary', self.get_description())
  872.         self._write_field(file, 'Home-page', self.get_url())
  873.         self._write_field(file, 'Author', self.get_contact())
  874.         self._write_field(file, 'Author-email', self.get_contact_email())
  875.         self._write_field(file, 'License', self.get_license())
  876.         if self.download_url:
  877.             self._write_field(file, 'Download-URL', self.download_url)
  878.         
  879.         long_desc = rfc822_escape(self.get_long_description())
  880.         self._write_field(file, 'Description', long_desc)
  881.         keywords = string.join(self.get_keywords(), ',')
  882.         if keywords:
  883.             self._write_field(file, 'Keywords', keywords)
  884.         
  885.         self._write_list(file, 'Platform', self.get_platforms())
  886.         self._write_list(file, 'Classifier', self.get_classifiers())
  887.         self._write_list(file, 'Requires', self.get_requires())
  888.         self._write_list(file, 'Provides', self.get_provides())
  889.         self._write_list(file, 'Obsoletes', self.get_obsoletes())
  890.  
  891.     
  892.     def _write_field(self, file, name, value):
  893.         if isinstance(value, unicode):
  894.             value = value.encode(PKG_INFO_ENCODING)
  895.         else:
  896.             value = str(value)
  897.         file.write('%s: %s\n' % (name, value))
  898.  
  899.     
  900.     def _write_list(self, file, name, values):
  901.         for value in values:
  902.             self._write_field(file, name, value)
  903.         
  904.  
  905.     
  906.     def get_name(self):
  907.         if not self.name:
  908.             pass
  909.         return 'UNKNOWN'
  910.  
  911.     
  912.     def get_version(self):
  913.         if not self.version:
  914.             pass
  915.         return '0.0.0'
  916.  
  917.     
  918.     def get_fullname(self):
  919.         return '%s-%s' % (self.get_name(), self.get_version())
  920.  
  921.     
  922.     def get_author(self):
  923.         if not self.author:
  924.             pass
  925.         return 'UNKNOWN'
  926.  
  927.     
  928.     def get_author_email(self):
  929.         if not self.author_email:
  930.             pass
  931.         return 'UNKNOWN'
  932.  
  933.     
  934.     def get_maintainer(self):
  935.         if not self.maintainer:
  936.             pass
  937.         return 'UNKNOWN'
  938.  
  939.     
  940.     def get_maintainer_email(self):
  941.         if not self.maintainer_email:
  942.             pass
  943.         return 'UNKNOWN'
  944.  
  945.     
  946.     def get_contact(self):
  947.         if not self.maintainer and self.author:
  948.             pass
  949.         return 'UNKNOWN'
  950.  
  951.     
  952.     def get_contact_email(self):
  953.         if not self.maintainer_email and self.author_email:
  954.             pass
  955.         return 'UNKNOWN'
  956.  
  957.     
  958.     def get_url(self):
  959.         if not self.url:
  960.             pass
  961.         return 'UNKNOWN'
  962.  
  963.     
  964.     def get_license(self):
  965.         if not self.license:
  966.             pass
  967.         return 'UNKNOWN'
  968.  
  969.     get_licence = get_license
  970.     
  971.     def get_description(self):
  972.         if not self.description:
  973.             pass
  974.         return 'UNKNOWN'
  975.  
  976.     
  977.     def get_long_description(self):
  978.         if not self.long_description:
  979.             pass
  980.         return 'UNKNOWN'
  981.  
  982.     
  983.     def get_keywords(self):
  984.         if not self.keywords:
  985.             pass
  986.         return []
  987.  
  988.     
  989.     def get_platforms(self):
  990.         if not self.platforms:
  991.             pass
  992.         return [
  993.             'UNKNOWN']
  994.  
  995.     
  996.     def get_classifiers(self):
  997.         if not self.classifiers:
  998.             pass
  999.         return []
  1000.  
  1001.     
  1002.     def get_download_url(self):
  1003.         if not self.download_url:
  1004.             pass
  1005.         return 'UNKNOWN'
  1006.  
  1007.     
  1008.     def get_requires(self):
  1009.         if not self.requires:
  1010.             pass
  1011.         return []
  1012.  
  1013.     
  1014.     def set_requires(self, value):
  1015.         import distutils.versionpredicate as distutils
  1016.         for v in value:
  1017.             distutils.versionpredicate.VersionPredicate(v)
  1018.         
  1019.         self.requires = value
  1020.  
  1021.     
  1022.     def get_provides(self):
  1023.         if not self.provides:
  1024.             pass
  1025.         return []
  1026.  
  1027.     
  1028.     def set_provides(self, value):
  1029.         value = [ v.strip() for v in value ]
  1030.         for v in value:
  1031.             import distutils.versionpredicate as distutils
  1032.             distutils.versionpredicate.split_provision(v)
  1033.         
  1034.         self.provides = value
  1035.  
  1036.     
  1037.     def get_obsoletes(self):
  1038.         if not self.obsoletes:
  1039.             pass
  1040.         return []
  1041.  
  1042.     
  1043.     def set_obsoletes(self, value):
  1044.         import distutils.versionpredicate as distutils
  1045.         for v in value:
  1046.             distutils.versionpredicate.VersionPredicate(v)
  1047.         
  1048.         self.obsoletes = value
  1049.  
  1050.  
  1051.  
  1052. def fix_help_options(options):
  1053.     """Convert a 4-tuple 'help_options' list as found in various command
  1054.     classes to the 3-tuple form required by FancyGetopt.
  1055.     """
  1056.     new_options = []
  1057.     for help_tuple in options:
  1058.         new_options.append(help_tuple[0:3])
  1059.     
  1060.     return new_options
  1061.  
  1062. if __name__ == '__main__':
  1063.     dist = Distribution()
  1064.     print 'ok'
  1065.  
  1066.